package Function;

import VWorldGameStudio.FCMonster;
import VWorldGameStudio.MstAttack;
import com.google.gson.Gson;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import utils.FCDataSingles;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;

public class BuildFCMonsterByExcelMain {
    //TODO 注意当前的excel工具只支持.xls，不支持.xlsx，要转换一下
    private static final String ITEM_FILE = "./重装机兵V-数据-怪物.xls";
    private String[] tags = new String[]{"名称", "英文ID", "种类1", "种类2", "奖励金额", "血量", "物理防御"//0-6
            , "速度", "触怪几率", "闪避", "经验", "逃跑", "主攻名", "主攻属性名"//7-13
            , "主攻持续", "主攻目标", "主攻人伤", "主攻车伤", "主攻破坏", "主攻破甲",  "主攻防","主攻动画ID"//14-20
            , "副攻名", "副攻属性名", "副攻持续", "副攻目标", "副攻人伤", "副攻车伤" //21-26
            , "副攻破坏", "副攻破甲",  "副攻防","副攻动画ID", "特攻名", "特攻属性名", "特攻持续", "特攻目标"//27-33
            , "特攻人伤", "特攻车伤", "特攻破坏", "特攻破甲", "特攻防", "特攻动画ID", "电麻痹", "毒"//34-40
            , "冰", "火", "眠", "酸","掉落物品ID","掉落几率","战斗道具1","战斗道具2","战斗道具3"//41-42
    ,"战斗二阶段ID","材质图片","描述特性","命中","甲值","主攻权重","副攻权重","特攻权重","道具权重","主攻属性伤害","副攻属性伤害","特攻属性伤害"
    ,"同场出现", "危险等级","攻击次数"};
    private HashMap<String, Integer> tagsAndIndexMap = new HashMap<>();
    private HashMap<String, String> chToEnTypeMaps = new HashMap<>();
    private Gson gson = new Gson();
    Sheet sheet;

    public static void main(String[] args) {
        BuildFCMonsterByExcelMain main = new BuildFCMonsterByExcelMain();
        main.init();
    }

    public BuildFCMonsterByExcelMain() {
        chToEnTypeMaps.put("酸性", "acid");
        chToEnTypeMaps.put("麻痹", "paralysis");
        chToEnTypeMaps.put("毒气", "gas");
        chToEnTypeMaps.put("火", "fire");
        chToEnTypeMaps.put("冷冻", "frozen");
        chToEnTypeMaps.put("睡眠", "sleep");
        chToEnTypeMaps.put("烟雾", "fog");
        chToEnTypeMaps.put("仿生", "bionic");
        chToEnTypeMaps.put("电子", "electron");
        chToEnTypeMaps.put("生化", "biochemistry");
        chToEnTypeMaps.put("坦克", "tank");
        chToEnTypeMaps.put("普通", "normal");
        chToEnTypeMaps.put("精英", "elite");
        chToEnTypeMaps.put("赏金", "wanted");
        chToEnTypeMaps.put("Boss", "boss");
    }

    public void init() {
        try {
            int ids = 1;
            File file = new File(ITEM_FILE);
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);
            sheet = wb.getSheet(0);
            boolean initKeyIndex = false;
            // sheet.getRows()返回该页的总行数
            for (int i = 0; i < sheet.getRows(); i++) {
                if (i == 1 || i == 0) {
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        Cell cell_1 = sheet.getCell(j, i);
                        if (cell_1 == null || cell_1.getContents().isEmpty()) {
                            continue;
                        }
                        String content = cell_1.getContents().trim();
                        for (String tag : tags) {
                            if (content.contains(tag)) {
                                System.out.println("TAG " + content + " j:" + j + " tag " + tag);
                                tagsAndIndexMap.put(tag, j);
                                break;
                            }
                        }
                    }
                    if (i == 1) {
                        initKeyIndex = true;
                    }
                }
                if (initKeyIndex) {
                    String chName = getCell(0, i);
                    if (chName == null) {
                        continue;
                    }
                    FCMonster fcMonster = new FCMonster();
                    fcMonster.setID(ids++);
                    fcMonster.setChName(chName);
                    fcMonster.setName(getCell(1,i));
                    fcMonster.setType(getCellDef(2,i,"tank"));
                    fcMonster.setType2(getCellDef(3,i,"normal"));
                    fcMonster.setBlood(getCellInt(5,i,10));
                    fcMonster.setDefense(getCellInt(6,i,10));
                    fcMonster.setSpeed(getCellInt(7,i,10));
                    fcMonster.setTriggerRate(getCellInt(8,i,3));
                    fcMonster.setDodge(getCellInt(9,i,0)/2);
                    int e = getCellInt(10,i,0);
                    if(e == 0){
                        e = (fcMonster.getBlood()+fcMonster.getDefense())/2;
                    }
                    fcMonster.setExperience(e);
                    int m = getCellInt(4,i,0);
                    if(m == 0){
                        m = (int)(e*0.6);
                    }
                    fcMonster.setMoney(m);
                    fcMonster.setEscape(getCellDouble(11,i,0));
                    MstAttack mstAttack = new MstAttack();
                    mstAttack.setName(getCell(12,i));
                    if(mstAttack.getName() != null){
                        fcMonster.setGun1(mstAttack);
                        mstAttack.setElement(getCellDef(13,i,"null"));
                        mstAttack.setRounds(getCellInt(14,i,1));
                        mstAttack.setTargetNum(getCellInt(15,i,1));
                        mstAttack.setHurtToMan(getCellInt(16,i,0));
                        mstAttack.setHurtToTank(getCellInt(17,i,0));
                        mstAttack.setDestruction(getCellInt(18,i,0));
                        mstAttack.setDesdenfese(getCellInt(19,i,0));
                        mstAttack.setDefense(getCellInt(20,i,-1));
                        mstAttack.setAnimationId(getCellDef(21,i,"null"));
                        mstAttack.setAttackWeight(getCellInt(58,i,3));
                        mstAttack.setElementHurt(getCellInt(62,i,0));
                    }
                    mstAttack = new MstAttack();
                    mstAttack.setName(getCell(22,i));
                    if(mstAttack.getName() != null){
                        fcMonster.setGun2(mstAttack);
                        mstAttack.setElement(getCellDef(23,i,"null"));
                        mstAttack.setRounds(getCellInt(24,i,1));
                        mstAttack.setTargetNum(getCellInt(25,i,1));
                        mstAttack.setHurtToMan(getCellInt(26,i,0));
                        mstAttack.setHurtToTank(getCellInt(27,i,0));
                        mstAttack.setDestruction(getCellInt(28,i,0));
                        mstAttack.setDesdenfese(getCellInt(29,i,0));
                        mstAttack.setDefense(getCellInt(30,i,-1));
                        mstAttack.setAnimationId(getCellDef(31,i,"null"));
                        mstAttack.setAttackWeight(getCellInt(59,i,3));
                        mstAttack.setElementHurt(getCellInt(63,i,0));
                    }
                    mstAttack = new MstAttack();
                    mstAttack.setName(getCell(32,i));
                    if(mstAttack.getName() != null){
                        fcMonster.setGun3(mstAttack);
                        mstAttack.setElement(getCellDef(33,i,"null"));
                        mstAttack.setRounds(getCellInt(34,i,1));
                        mstAttack.setTargetNum(getCellInt(35,i,1));
                        mstAttack.setHurtToMan(getCellInt(36,i,0));
                        mstAttack.setHurtToTank(getCellInt(37,i,0));
                        mstAttack.setDestruction(getCellInt(38,i,0));
                        mstAttack.setDesdenfese(getCellInt(39,i,0));
                        mstAttack.setDefense(getCellInt(40,i,-1));
                        mstAttack.setAnimationId(getCellDef(41,i,"null"));
                        mstAttack.setAttackWeight(getCellInt(60,i,3));
                        mstAttack.setElementHurt(getCellInt(64,i,0));
                    }
                    fcMonster.setParalysisResistance(getCellInt(42,i,0));
                    fcMonster.setGasResistance(getCellInt(43,i,0));
                    fcMonster.setFrozenResistance(getCellInt(44,i,0));
                    fcMonster.setFireResistance(getCellInt(45,i,0));
                    fcMonster.setSleepResistance(getCellInt(46,i,0));
                    fcMonster.setAcidResistance(getCellInt(47,i,0));
                    fcMonster.setDropItemID(getCellDef(48,i, "null"));
                    fcMonster.setDropPro(getCellDouble(49, i,0.05));
                    String fightItem = getCell(50,i);
                    if(fightItem != null){
                        fcMonster.getKeepFcItems().add(fightItem);
                    }
                    fightItem = getCell(51,i);
                    if(fightItem != null){
                        fcMonster.getKeepFcItems().add(fightItem);
                    }
                    fightItem = getCell(52,i);
                    if(fightItem != null){
                        fcMonster.getKeepFcItems().add(fightItem);
                    }
                    fcMonster.setUpgradeMonsterID(getCell(53,i));
                    String url = getCellDef(54, i, "default");
                    if (url != null) {
                        if (!url.contains("png")) {
                            url = url + ".png";
                        }
                        fcMonster.setPngUrl("./material/png/monster/" + url);
                    }
                    fcMonster.setDepict(getCellDef(55, i, "无"));
                    fcMonster.setHitRate(getCellInt(56, i, 100));
                    fcMonster.setPlateArmour(getCellInt(57, i, 0));
                    fcMonster.setUseItemsWeight(getCellInt(61,i,3));
                    fcMonster.setPartnerFollow(getCellInt(65,i,0));
                    fcMonster.setDangerLevel(getCellInt(66,i,1));
                    fcMonster.setAttackTimes(getCellDouble(67,i,1));
                    System.out.println("---->" + gson.toJson(fcMonster));
                    FCDataSingles.getWriter().writeMonsterData(fcMonster);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getIndex(int index) {
        Integer ret = tagsAndIndexMap.get(tags[index]);
         //  System.out.println("getIndex "+index+" "+ret+"  "+tags[index]);
        if (ret == null) {
            return index;
        }
        return ret;
    }

    private String getEnWords(String c) {
        if (chToEnTypeMaps.containsKey(c)) {
            return chToEnTypeMaps.get(c);
        }
        return c;
    }

    public String getCell(int index, int i) {
        index = getIndex(index);
        if (index < 0) {
            System.out.println("getCell index < 0");
            return null;
        }

        Cell cell = sheet.getCell(index, i);
        if (cell == null || cell.getContents() == null || cell.getContents().isEmpty()) {
            return null;
        }
        return getEnWords(cell.getContents().trim());
    }

    public String getCellDef(int index, int i, String def) {
        String d = getCell(index, i);
        if (d != null) {
            return getEnWords(d);
        }
        return def;
    }

    public int getCellInt(int index, int i, int def) {
        String d = getCell(index, i);
        if (d != null) {
            return Integer.parseInt(d);
        }
        return def;
    }

    public double getCellDouble(int index, int i, double def) {
        String d = getCell(index, i);
        if (d != null) {
            return Double.parseDouble(d);
        }
        return def;
    }
}
